<?php
class UserModel{
	protected $_name = 'eway_user';
	protected $_dbAdapter;

	function UserModel(){
		$this->_dbAdapter = Zend_Registry::get(Constant::DB_ADAPTER);
	}

	public function checkUserExist($mobile){
		$sql = "SELECT * FROM ".$this->_name." WHERE usr_mobile=?";
		$result = $this->_dbAdapter->fetchRow($sql, $mobile);
		return $result;
	}

	public function checkUserByAccountAndPassword($username, $password){
		$sql = "SELECT * FROM ".$this->_name." WHERE usr_mobile=? AND usr_password=?";
		$result = $this->_dbAdapter->fetchRow($sql, array($username, $password));
		return $result;
	}

	public function insertUser($userObject = array()){
		$this->_dbAdapter->insert($this->_name, $userObject);
	}

	function getTotalRecords($activated) {
		$const_admin = Constant::ADMINISTRATOR;
		$const_editor = Constant::EDITOR;
		$sql = "SELECT COUNT(*) FROM " . $this->_name . " WHERE usr_activated=? AND ( usr_role != ? OR usr_role != ? )";
		$result = $this->_dbAdapter->fetchCol ( $sql, array($activated, $const_admin, $const_editor) );
		return $result [0];
	}

	public function getAllUser($activated = 0, $currentPages, $recordPerPage) {
		$offset = ($currentPages - 1) * $recordPerPage;
		$const_admin = Constant::ADMINISTRATOR;
		$const_editor = Constant::EDITOR;
		$sql = "SELECT * FROM " . $this->_name ." "
		. "WHERE usr_activated=? AND ( usr_role != ? OR usr_role != ? ) "
		. "ORDER BY usr_created_date DESC LIMIT $offset,$recordPerPage";
		$result = $this->_dbAdapter->fetchAll ( $sql, array($activated, $const_admin, $const_editor) );
		return $result;
	}

	function getTotalAdministratorRecords() {
		$const_admin = Constant::ADMINISTRATOR;
		$const_editor = Constant::EDITOR;
		$sql = "SELECT COUNT(*) FROM " . $this->_name . " WHERE usr_role=? OR usr_role=?";
		$result = $this->_dbAdapter->fetchCol ( $sql, array($const_admin, $const_editor) );
		return $result [0];
	}

	public function getAllAdministrator($currentPages, $recordPerPage) {
		$offset = ($currentPages - 1) * $recordPerPage;
		$const_admin = Constant::ADMINISTRATOR;
		$const_editor = Constant::EDITOR;
		$sql = "SELECT * FROM " . $this->_name . " WHERE usr_role=? OR usr_role=? ORDER BY usr_created_date ASC LIMIT $offset,$recordPerPage";
		$result = $this->_dbAdapter->fetchAll ( $sql, array($const_admin, $const_editor) );
		return $result;
	}

	public function update($user=array(), $user_id){
		$where = "usr_id=".$user_id;
		$this->_dbAdapter->update($this->_name, $user, $where);
	}

	public function delete($user_id){
		$where = "usr_id=".(int)$user_id;
		$this->_dbAdapter->delete($this->_name, $where);
	}

	public function getUserById($user_id){
		$sql = "SELECT * FROM ".$this->_name." WHERE usr_id=?";
		$result = $this->_dbAdapter->fetchRow($sql, $user_id);
		return $result;
	}

	public function checkPassword($user_mobile, $password){
		$sql = "SELECT * FROM ".$this->_name." WHERE usr_mobile=? AND usr_password=?";
		$result = $this->_dbAdapter->fetchRow($sql, array($user_mobile, $password));
		if ($result)
		return true;
		else
		return false;
	}

}// End class.